package com.slz3.business.module.memberManagement.drawTicket.steward;

import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.module.memberManagement.drawTicket.domain.DrawTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @FileName: MemMemberSynr
 * @Description:会员抽奖券同步
 */

@Component
@Slf4j
public class DrawTicketBackSync {

    /**
     * 会员抽奖券同步
     */
    public static void syncData(){

        try {
            Object obj = Jedis.db0.leftPop(RK.DRAW_TICKET_LIST);
            Map<String, Object> dataMap = (Map<String, Object>) obj;
            dataMap.remove("createTime");
            DrawTicket entity = ConvertUtil.objToObj(obj,DrawTicket.class);
            log.info("消费消息:" + obj.toString());
            //处理完删除set集合
            Services.drawTicket.save(entity);
            RedisClient.redisTemplate.opsForSet().pop(RK.DRAW_TICKET_LIST+"Set");
        }catch (Exception e){
            //主数据set 集合
            Set userScore= Jedis.db0.getSet(RK.SAVE_ORDER.concat("Set"));
            Iterator iterator = userScore.iterator();
            //消费逻辑
            log.info("消费消息:" + iterator.toString());
            //遍历set集合
            while(iterator.hasNext()){
                Map<String, Object> dataMap =(Map<String,Object>) iterator.next();
                dataMap.remove("createTime");
                DrawTicket entity = ConvertUtil.objToObj(dataMap,DrawTicket.class);
                Steward.initNullField(entity);
                //会员抽奖券保存
                Services.drawTicket.save(entity);
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.DRAW_TICKET_LIST+"Set");
        }

    }



}